from django.shortcuts import render
from rest_framework import status
from rest_framework.generics import ListAPIView, CreateAPIView, UpdateAPIView, DestroyAPIView

# Create your views here.
from rest_framework.response import Response
from rest_framework.status import HTTP_204_NO_CONTENT
from rest_framework.viewsets import ViewSet

from areas import serializers
from areas.models import Area
from areas.serializers import AreasSerializer, AddressSerializer
from rest_framework_extensions.cache.mixins import CacheResponseMixin
from users.models import Address
from rest_framework.decorators import action


class AreasView(CacheResponseMixin, ListAPIView):

    serializer_class = AreasSerializer

    queryset = Area.objects.filter(parent=None)
    pagination_class = None

    # def get(self, request):
    #     # 获取省的数据对象
    #
    #     # 序列化返回

class AreaView(CacheResponseMixin, ListAPIView):

    serializer_class = AreasSerializer

    def get_queryset(self):
        pk = self.kwargs['pk']
        return Area.objects.filter(parent_id=pk)


# psot addresses/
class AddressView(CreateAPIView, ListAPIView, UpdateAPIView):
    serializer_class = AddressSerializer
    # queryset = Address.objects.filter(is_deleted=False)

    def get_queryset(self):
        return Address.objects.filter(is_deleted=False,user=self.request.user)

    def get(self, request, *args, **kwargs):
        queryset = self.filter_queryset(self.get_queryset())

        # page = self.paginate_queryset(queryset)
        # if page is not None:
        #     serializer = self.get_serializer(page, many=True)
        #     return self.get_paginated_response(serializer.data)

        serializer = self.get_serializer(queryset, many=True)
        return Response({'addresses':serializer.data})




# 修改和删除地址
class DestroyAndDelAddressView(DestroyAPIView, UpdateAPIView,ViewSet):
    serializer_class = AddressSerializer

    def get_queryset(self):
        return Address.objects.filter(is_deleted=False,user=self.request.user)

    @action(methods=['delete'], detail=True)
    def destroy(self, request, *args, **kwargs):
        """处理删除"""
        address = self.get_object()

        # 进行逻辑删除
        address.is_deleted = True
        address.save()

        return Response(status=HTTP_204_NO_CONTENT)

    # put /addresses/pk/status/
    @action(methods=['put'], detail=True)
    def status(self, request, pk=None):
        """
        设置默认地址
        """
        address = self.get_object()
        request.user.default_address = address
        request.user.save()
        return Response({'message': 'OK'}, status=status.HTTP_200_OK)



        # put /addresses/pk/title/
        # 需要请求体参数 title
    @action(methods=['put'], detail=True)
    def title(self, request, pk=None):
        """
        修改标题
        """
        address = self.get_object()
        serializer = serializers.AddressTitleSerializer(instance=address, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data)